package com.situ.mall.web.controller;


import com.situ.mall.common.entity.Cart;
import com.situ.mall.common.entity.Member;
import com.situ.mall.common.exception.NoLoginException;
import com.situ.mall.common.service.ICartService;
import com.situ.mall.common.util.RequestUtil;

import com.situ.mall.common.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @GetMapping("/list")
    public String page(Model model) {

        List<CartVO> carts = cartService.select(RequestUtil.getCurrentMemberId());
//        Integer AllAmount = 0;
//        BigDecimal AllTotalPrice = new BigDecimal("0");

        for (CartVO c : carts){
            BigDecimal count = new BigDecimal(c.getAmount());
            c.setTotalPrice(c.getPrice().multiply(count));
//            AllAmount+=c.getAmount();
//            AllTotalPrice = AllTotalPrice.add(c.getTotalPrice());

        }


        model.addAttribute("product", carts);
        return "cart";
    }

    @PostMapping("/save")
    @ResponseBody
    public void save(Integer pId, Integer count) {
        Object o = RequestUtil.getCurrent();//得到当前登录的用户
        Member m = (Member) o;
        if (o == null) {//没登陆
            throw new NoLoginException();
        } else {
            Cart cart = cartService.selectByPId(pId, m.getId());

            if (cart == null) {
                Cart c = new Cart();
                c.setProductId(pId);
                c.setCreateTime(LocalDateTime.now());
                c.setMemberId(m.getId());
                c.setAmount(count);
                cartService.saveOrUpdate(c);

            } else {
                cart.setAmount(cart.getAmount() + count);
                cartService.saveOrUpdate(cart);
                System.out.println(cart);
            }
        }

    }

    @PostMapping("/deleteOne")
    @ResponseBody
    public void deleteOne(Integer pId) {
        Object o = RequestUtil.getCurrent();//得到当前登录的用户
        Member m = (Member) o;
        cartService.deleteOne(pId, m.getId());
    }

    @PostMapping("/addOne")
    @ResponseBody
    public void addOne(Integer pId) {
        Object o = RequestUtil.getCurrent();//得到当前登录的用户
        Member m = (Member) o;
        cartService.addOne(pId, m.getId());
    }


    @PostMapping("/reduceOne")
    @ResponseBody
    public void reduceOne(Integer pId) {
        Object o = RequestUtil.getCurrent();//得到当前登录的用户
        Member m = (Member) o;
        cartService.reduceOne(pId, m.getId());
    }

    @PostMapping("/deleteAll")
    @ResponseBody
    public void deleteAll() {

    }


}
